/*
 * Copyright 2008 Karel Klic.
 * This file is part of Oslavanský Bot.
 * 
 * Oslavanský Bot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package categorybot;

import java.util.*;
import java.util.regex.*;

/**
 * Seznam kategorií pro jeden slovní druh.
 * Příklad kategorie: Kategorie:Anglická substantiva.
 * 
 * Třída není napsaná genericky. Zobecni ji, pokud se to ukáže jako nutnost.
 */
public class CategoryList {

  String id;
  String idForm;
  Pattern pattern;
  Pattern patternForm;

  /**
   * Standardni konstruktor.
   * @param id
   * Zakladni kategorie.
   * @param idForm
   * Specialni kategorie, do ktere spadaji ruzne tvary slov spadajicich do 
   * zakladni kategorie.
   */
  public CategoryList(String id, String idForm) {
    this.id = id;
    this.idForm = idForm;
    pattern = PatternFactory.getCategoryPattern(id);
    patternForm = PatternFactory.getCategoryPattern(idForm);
  }

  /**
   * Zajisti, ze text obsahuje [[Kategorie:Nejaka nase kategorie]].
   * @param text
   * Cely text slovicka.
   * @param startTypeArea
   * Zacatek naseho slovniho druhu. Prvni znak.
   * @param endTypeArea
   * Ukazatel za posledni znak naseho slovniho druhu.
   * @return
   * True pokud byl nejaky problem se slovickem.
   */
  public ArrayList<SolveCategoryResult> solveContainsCategory(String text,
          int startTypeArea, int endTypeArea) {
    ArrayList<SolveCategoryResult> result = new ArrayList<SolveCategoryResult>();

    // Podivame se, jestli se ke slovicku hodi zvlastni forma.
    String typeText = text.substring(startTypeArea, endTypeArea);
    boolean isDerivativeForm = DerivativeFormDetector.getInstance().isDerivativeForm(typeText);

    if (isDerivativeForm) {
      if (!patternForm.matcher(text).find()) {
        SolveCategoryResult addRequired = new SolveCategoryResult(
                SolveCategoryResult.ChangeType.Add, idForm);
        result.add(addRequired);
      }
      // Sometimes the same word is a derivative form of one word and basic 
      // word in the same time in the same word type. Do not uncomment this.
      //
      //if (pattern.matcher(text).find()) {
      //  SolveCategoryResult removeRequired = new SolveCategoryResult(
      //          SolveCategoryResult.ChangeType.Remove, id);
      //  result.add(removeRequired);
      //}
    } else { // not a derivative form
      // Sometimes the same word is a derivative form of one word and basic 
      // word in the same time in the same word type. Do not uncomment this.
      //
      //if (patternForm.matcher(text).find()) {
      //  SolveCategoryResult removeRequired = new SolveCategoryResult(
      //          SolveCategoryResult.ChangeType.Remove, idForm);
      //  result.add(removeRequired);
      //}
      if (!pattern.matcher(text).find()) {
        SolveCategoryResult addRequired = new SolveCategoryResult(
                SolveCategoryResult.ChangeType.Add, id);
        result.add(addRequired);
      }
    }
    return result;
  }

  /**
   * Ensures that the text does not contain categories represented by this
   * CategoryList.
   * @param text
   * @return
   */
  public ArrayList<SolveCategoryResult> solveNotContainsCategory(String text) {
    ArrayList<SolveCategoryResult> result = new ArrayList<SolveCategoryResult>();

    if (pattern.matcher(text).find()) {
      SolveCategoryResult removeRequired = new SolveCategoryResult(
              SolveCategoryResult.ChangeType.Remove, id);
      result.add(removeRequired);
    }

    if (patternForm.matcher(text).find()) {
      SolveCategoryResult removeRequired = new SolveCategoryResult(
              SolveCategoryResult.ChangeType.Remove, idForm);
      result.add(removeRequired);
    }
    return result;
  }
}
